#include <qqml.h>
#include <QQmlContext>
#include "utilsprivate.h"

static void groupByIdImp(QObject* root,QMap<QString,QObject*>& ret){
    auto ctx = qmlContext(root);

    if(ctx)
    {
        QString id = ctx->nameForObject(root);
        if(!id.isEmpty())
        {
            ret[id] = root;
        }

    }

    for(auto i:root->children())
    {
        groupByIdImp(i,ret);
    }
}

UtilsPrivate::IdMap UtilsPrivate::groupById(QObject *root)
{
    QMap<QString,QObject*> ret;
    groupByIdImp(root,ret);
    return ret;
}

bool UtilsPrivate::isBase(QString className, const QMetaObject *meta)
{
    while (meta) {
        if(meta->className() == className.toUtf8())
        {
            return true;
        }

        meta = meta->superClass();
    }
    return false;
}
